app.controller('shopBindUserCtrl',['$scope','$state','utilService','toaster','$modal','shopBindUserService',function($scope,$state,utilService,toaster,$modal,shopBindUserService){
    $scope.addShow=false;

    $scope.phone;

    $scope.sendUserBindCode=function(id){
        shopBindUserService.sendUserBindCode($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    openModel(id);
                    break;
                case 680:
                    toaster.pop('warnning', "提示ʾ", result.message);
                    break
                default :
                    $scope.searchState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    var openModel=function(id){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/shop/bind_popup.html',
            controller: 'shopBindUserPopupCtrl',
            size:'sm',
            scope: $scope,
            resolve:{
                data:function(){
                    return {id:id,phone:$scope.searchUser.mobilePhone};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.searchUser=null;
            $scope.getShopBindUser();
        }, function (reason) {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

    //查询用户信息
    $scope.searchUser;
    $scope.searchState=false;
    $scope.findByMobilePhone=function(){
        if(angular.isUndefined($scope.phone) || $scope.phone==null){
            return;
        }
        if($scope.searchState){
            return;
        }
        $scope.searchState=true;
        shopBindUserService.findByMobilePhone($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.searchUser=result.data;
                    $scope.searchState=false;
                    break;
                case 663:
                    $scope.searchState=false;
                    $scope.searchUser=null;
                    break;
                default :
                    $scope.searchState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    //查询已经绑定的用户
    $scope.bindUsers=new Array();
    $scope.searchBindUserState=false;
    $scope.getShopBindUser=function(){
        if($scope.searchBindUserState){
            return;
        }

        $scope.searchBindUserState=true;

        shopBindUserService.getShopBindUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.searchBindUserState=false;
                    $scope.bindUsers=result.data;
                    break;
                default :
                    $scope.searchBindUserState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    $scope.getShopBindUser();



    //解绑商品
    $scope.sendUserUnBindCode=function(id,mobilePhone){
        shopBindUserService.sendUserUnBindCode($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    openUnBindModel(id,mobilePhone);
                    break;
                case 680:
                    toaster.pop('warnning', "提示ʾ", result.message);
                    break
                default :
                    $scope.searchState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    var openUnBindModel=function(id,mobilePhone){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/shop/unbind_popup.html',
            controller: 'shopUnBindUserPopupCtrl',
            size:'sm',
            scope: $scope,
            resolve:{
                data:function(){
                    return {id:id,phone:mobilePhone};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.getShopBindUser();
        }, function (reason) {
            console.log(reason);//点击空白区域，总会输出backdrop click，点击取消，则会暑促cancel
            $log.info('Modal dismissed at: ' + new Date());
        });
    }



}]);


app.controller('shopBindUserPopupCtrl', ['$scope', '$modalInstance','$state','shopBindUserService','toaster','data', function($scope, $modalInstance,$state,shopBindUserService,toaster,data) {
    $scope.cancel=function(){
        $modalInstance.close("cancel");
    }

    $scope.id=data.id;
    $scope.phone=data.phone;
    $scope.code;

    $scope.shopBindUser=function(){
        shopBindUserService.shopBindUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $modalInstance.close("success");
                    break;
                case 624:
                case 663:
                case 647:
                case 408:
                case 691:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);


app.controller('shopUnBindUserPopupCtrl', ['$scope', '$modalInstance','$state','shopBindUserService','toaster','data', function($scope, $modalInstance,$state,shopBindUserService,toaster,data) {
    $scope.cancel=function(){
        $modalInstance.close("cancel");
    }

    $scope.id=data.id;
    $scope.phone=data.phone;
    $scope.code;

    $scope.shopUnBindUser=function(){
        shopBindUserService.shopUnBindUser($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $modalInstance.close("success");
                    break;
                case 624:
                case 663:
                case 647:
                case 408:
                case 691:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }


}]);